package org.yang.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.yang.admin.common.convention.result.Result;
import org.yang.admin.common.convention.result.Results;
import org.yang.admin.dto.req.UserLoginReqDTO;
import org.yang.admin.dto.req.UserRegisterReqDTO;
import org.yang.admin.dto.resp.UserActualRespDTO;
import org.yang.admin.dto.resp.UserLoginRespDTO;
import org.yang.admin.dto.resp.UserRespDTO;
import org.yang.admin.service.UserService;

/**
 * @Description: TODO
 * @Author: YangTing
 * @Date: 2024-07-02 20:54
 */
@RestController
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    //注册
    @PostMapping("/api/short-link/admin/v1/user")
    public Result<Void> register(@RequestBody UserRegisterReqDTO requestParam){
        userService.register(requestParam);
        return Results.success();
    }

    //登录
    @PostMapping("/api/short-link/admin/v1/user/login")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO requestParam){
        return Results.success(userService.login(requestParam));
    }

    //退出登录
    @GetMapping("/api/short-link/admin/v1/user/logout")
    public Result<Void> logout(@RequestParam("username") String username, @RequestParam("token") String token){
        userService.logout(username,token);
        return Results.success();
    }

    //根据用户名获取用户加密后的信息
    @GetMapping("/api/short-link/admin/v1/user/{username}")
    public Result<UserRespDTO> getUserByUsername(@PathVariable("username") String username) {
        return Results.success(userService.getUserByUsername(username));
    }

    //根据用户名获取用户真实信息
    @GetMapping("/api/short-link/admin/v1/actual/user/{username}")
    public Result<UserActualRespDTO> getActualUserByUsername(@PathVariable("username") String username) {
        return Results.success(BeanUtil.toBean(userService.getUserByUsername(username),UserActualRespDTO.class));
    }

    //根据用户名判断系统中是否已经存在该用户
    @GetMapping("/api/short-link/admin/v1/user/has-username")
    public Result<Boolean> hasUsername(@RequestParam("username") String username) {
        return Results.success(userService.hasUsername(username));
    }

    //根据用户名判断该用户是否已经登录
    @GetMapping("/api/short-link/admin/v1/user/hasLogin")
    public Result<Boolean> hasLogin(@RequestParam("username") String username, @RequestParam("token") String token){
        return Results.success(userService.hasLogin(username, token));
    }
}
